package org.cllib.math;

/* MathFP - Decompiled by JODE
 * Visit http://jode.sourceforge.net/
 */
public abstract class MathFP {
	private static int _fbits = 12;
	private static int _digits = 4;
	private static int _one = 4096;
	private static int _fmask = 4095;
	private static int _dmul = 10000;
	private static int _flt = 0;
	private static int _pi = 12868;
	private static int[] e = { _one, 11134, 30266, 82270, 223636 };
	public static int PI = _pi;
	public static int E = e[1];
	public static final int MAX_VALUE = 2147483647;
	public static final int MIN_VALUE = -2147483647;
	
	public static int setPrecision(int i) {
		if (i > 12 || i < 0)
			return _digits;
		_fbits = i;
		_one = 1 << i;
		_flt = 12 - i;
		_digits = 0;
		_dmul = 1;
		_fmask = _one - 1;
		PI = _pi >> _flt;
		E = e[1] >> _flt;
		int i_0_ = _one;
		while (i_0_ != 0) {
			i_0_ /= 10;
			_digits++;
			_dmul *= 10;
		}
		return _digits;
	}

	public static int toInt(int i) {
		if (i < 0)
			return -(round(-i, 0) >> _fbits);
		return round(i, 0) >> _fbits;
	}
	
	public static int convert(int i, int i_1_) {
		int i_2_ = i < 0 ? -1 : 1;
		if (abs(i_1_) < 13) {
			if (_fbits < i_1_)
				i = i + i_2_ * (1 << (i_1_ - _fbits >> 1)) >> i_1_ - _fbits;
			else
				i <<= _fbits - i_1_;
		}
		return i;
	}
	
	public static int toFP(String string) {
		int i = 0;
		if (string.charAt(0) == '-')
			i = 1;
		String string_3_ = "-1";
		int i_4_ = string.indexOf('.');
		if (i_4_ >= 0) {
			for (string_3_ = string.substring(i_4_ + 1, string.length()); string_3_
					.length() < _digits; string_3_ += "0") {
				/* empty */
			}
			if (string_3_.length() > _digits)
				string_3_ = string_3_.substring(0, _digits);
		} else
			i_4_ = string.length();
		int i_5_ = 0;
		if (i != i_4_)
			i_5_ = Integer.parseInt(string.substring(i, i_4_));
		int i_6_ = Integer.parseInt(string_3_) + 1;
		int i_7_ = (i_5_ << _fbits) + (i_6_ << _fbits) / _dmul;
		if (i == 1)
			i_7_ = -i_7_;
		return i_7_;
	}
	
	public static int round(int i, int i_13_) {
		int i_14_ = 10;
		for (int i_15_ = 0; i_15_ < i_13_; i_15_++)
			i_14_ *= 10;
		i_14_ = div(toFP(5), toFP(i_14_));
		if (i < 0)
			i_14_ = -i_14_;
		return i + i_14_;
	}
	
	private static void hey(String s){
		System.out.println("************************************** HEY PAY ATTENTION **************************************");
		System.out.println(s);
	}
	
	public static int mul(int i, int i_16_) {
		boolean bool = false;
		int i_17_ = _fbits;
		int i_18_ = _fmask;
		if ((i & i_18_) == 0)
			return (i >> i_17_) * i_16_;
		if ((i_16_ & i_18_) == 0)
			return i * (i_16_ >> i_17_);
		if (i < 0 && i_16_ > 0 || i > 0 && i_16_ < 0)
			bool = true;
		if (i < 0)
			i = -i;
		if (i_16_ < 0)
			i_16_ = -i_16_;
		
		int _adjust_count = 0;
		for (/**/; max(i, i_16_) >= 1 << (31 - i_17_); i_17_--) {
			_adjust_count ++;
			i >>= 1;
			i_16_ >>= 1;
		
			i_18_ >>= 1;
		}
		
		if(_adjust_count > 0){
			hey("_adjust_count for mul [" + _adjust_count + "] number of decimal bits[" + i_17_ + "]");
		}
		/**
		 * |a|b|
		 * |c|d|
		 */
		
		/** a * c << 12 **/
		int i_19_ = (i >> i_17_) * (i_16_ >> i_17_) << i_17_;
		
		/** b * d >> 12 carry **/
		int i_20_ = (i & i_18_) * (i_16_ & i_18_) >> i_17_;
		
		/** a * d >> 12 + carry of b * d **/
		int i_21_ = (i & (i_18_ ^ 0xffffffff)) * (i_16_ & i_18_) >> i_17_;
		
		/** b * c the most weird part **/
		int _v = ((i & i_18_) * (i_16_ & (i_18_ ^ 0xffffffff)) >> i_17_) << (_fbits - i_17_);
		
		i_19_ = i_19_ + i_20_ + i_21_ + _v;
		if (i_19_ < 0)
			throw new ArithmeticException("Overflow");
		return bool ? -i_19_ : i_19_;
	}

	public static int div(int i, int i_21_) {
		boolean bool = false;
		int i_22_ = _fbits;
		if (i_21_ == _one)
			return i;
		if ((i_21_ & _fmask) == 0)
			return i / (i_21_ >> i_22_);
		if (i < 0 && i_21_ > 0 || i > 0 && i_21_ < 0)
			bool = true;
		if (i < 0)
			i = -i;
		if (i_21_ < 0)
			i_21_ = -i_21_;
		for (/**/; max(i, i_21_) >= 1 << 31 - i_22_; i_22_--) {
			i >>= 1;
			i_21_ >>= 1;
		}
		
		int i_23_ = (i << i_22_) / i_21_ << _fbits - i_22_;
		return bool ? -i_23_ : i_23_;
	}
	
	public static int exp(int i) {
		if (i == 0)
			return _one;
		boolean bool = i < 0;
		i = abs(i);
		int i_36_ = i >> _fbits;
		int i_37_ = _one;
		for (int i_38_ = 0; i_38_ < i_36_ / 4; i_38_++)
			i_37_ = mul(i_37_, e[4] >> _flt);
		if (i_36_ % 4 > 0)
			i_37_ = mul(i_37_, e[i_36_ % 4] >> _flt);
		i &= _fmask;
		if (i > 0) {
			int i_39_ = _one;
			int i_40_ = 0;
			int i_41_ = 1;
			for (int i_42_ = 0; i_42_ < 16; i_42_++) {
				i_40_ += i_39_ / i_41_;
				i_39_ = mul(i_39_, i);
				i_41_ *= i_42_ + 1;
				if (i_41_ > i_39_ || i_39_ <= 0 || i_41_ <= 0)
					break;
			}
			i_37_ = mul(i_37_, i_40_);
		}
		if (bool)
			i_37_ = div(_one, i_37_);
		return i_37_;
	}

	public static int log(int i) {
		if (i <= 0)
			throw new ArithmeticException("Bad Input");
		int i_43_ = 0;
		boolean bool = false;
		int i_44_ = 0;
		while (i >= _one << 1) {
			i >>= 1;
			i_44_++;
		}
		int i_45_ = i_44_ * (2839 >> _flt);
		int i_46_ = 0;
		if (i < _one)
			return -log(div(_one, i));
		i -= _one;
		for (int i_47_ = 1; i_47_ < 20; i_47_++) {
			int i_48_;
			if (i_43_ == 0)
				i_48_ = i;
			else
				i_48_ = mul(i_43_, i);
			if (i_48_ == 0)
				break;
			i_46_ = i_46_ + (i_47_ % 2 == 0 ? -1 : 1) * i_48_ / i_47_;
			i_43_ = i_48_;
		}
		return i_45_ + i_46_;
	}

	public static int pow(int i, int i_49_) {
		boolean bool = i_49_ < 0;
		int i_50_ = _one;
		i_49_ = abs(i_49_);
		int i_51_ = i_49_ >> _fbits;
		while (i_51_-- > 0)
			i_50_ = mul(i_50_, i);
		if (i_50_ < 0)
			throw new ArithmeticException("Overflow");
		if (i != 0)
			i_50_ = mul(i_50_, exp(mul(log(i), i_49_ & _fmask)));
		else
			i_50_ = 0;
		if (bool)
			return div(_one, i_50_);
		return i_50_;
	}

	public static int atan2(int i, int i_52_) {
		boolean bool = false;
		int i_53_;
		if (i_52_ > 0)
			i_53_ = atan(div(i, i_52_));
		else if (i_52_ < 0)
			i_53_ = (i_52_ < 0 ? -PI : PI) - atan(abs(div(i, i_52_)));
		else {
			if (i_52_ == 0 && i == 0)
				throw new ArithmeticException("Bad Input");
			i_53_ = (i_52_ < 0 ? -PI : PI) / 2;
		}
		return i_53_;
	}
	
	public static int sin(int i) {
		int i_29_ = mul(i, div(toFP(180), PI));
		i_29_ %= toFP(360);
		if (i_29_ < 0)
			i_29_ = toFP(360) + i_29_;
		int i_30_ = i_29_;
		if (i_29_ >= toFP(90) && i_29_ < toFP(270))
			i_30_ = toFP(180) - i_29_;
		else if (i_29_ >= toFP(270) && i_29_ < toFP(360))
			i_30_ = -(toFP(360) - i_29_);
		int i_31_ = i_30_ / 90;
		int i_32_ = mul(i_31_, i_31_);
		int i_33_ = mul(mul(mul(mul(-18 >> _flt, i_32_) + (326 >> _flt), i_32_)
				- (2646 >> _flt), i_32_)
				+ (6434 >> _flt), i_31_);
		return i_33_;
	}

	public static int asin(int i) {
		if (abs(i) > _one)
			throw new ArithmeticException("Bad Input");
		boolean bool = i < 0;
		if (i < 0)
			i = -i;
		int i_34_ = (mul(mul(mul(mul(35 >> _flt, i) - (146 >> _flt), i)
				+ (347 >> _flt), i)
				- (877 >> _flt), i) + (6434 >> _flt));
		int i_35_ = PI / 2 - mul(sqrt(_one - i), i_34_);
		return bool ? -i_35_ : i_35_;
	}
	
	public static int sqrt(int i, int i_26_) {
		if (i < 0)
			throw new ArithmeticException("Bad Input");
		if (i == 0)
			return 0;
		int i_27_ = i + _one >> 1;
		for (int i_28_ = 0; i_28_ < i_26_; i_28_++)
			i_27_ = i_27_ + div(i, i_27_) >> 1;
		if (i_27_ < 0)
			throw new ArithmeticException("Overflow");
		return i_27_;
	}
	
	public static int getPrecision() {
		return _fbits;
	}

	public static int toFP(int i) {
		return i << _fbits;
	}

	public static String toString(int i) {
		boolean bool = false;
		if (i < 0) {
			bool = true;
			i = -i;
		}
		int i_8_ = i >> _fbits;
		int i_9_ = _dmul * (i & _fmask) >> _fbits;
		String string;
		for (string = Integer.toString(i_9_); string.length() < _digits; string = "0"
				+ string) {
			/* empty */
		}
		return (bool ? "-" : "") + Integer.toString(i_8_) + "." + string;
	}

	public static String toString(int i, int i_10_) {
		if (i_10_ > _digits)
			i_10_ = _digits;
		String string = toString(round(i, i_10_));
		return string.substring(0, string.length() - _digits + i_10_);
	}

	public static int max(int i, int i_11_) {
		return i < i_11_ ? i_11_ : i;
	}

	public static int min(int i, int i_12_) {
		return i_12_ < i ? i_12_ : i;
	}

	public static int add(int i, int i_24_) {
		return i + i_24_;
	}

	public static int sub(int i, int i_25_) {
		return i - i_25_;
	}

	public static int abs(int i) {
		if (i < 0)
			return -i;
		return i;
	}

	public static int sqrt(int i) {
		return sqrt(i, 16);
	}

	public static int cos(int i) {
		return sin(PI / 2 - i);
	}

	public static int acos(int i) {
		return PI / 2 - asin(i);
	}

	public static int tan(int i) {
		return div(sin(i), cos(i));
	}

	public static int cot(int i) {
		return div(cos(i), sin(i));
	}

	public static int atan(int i) {
		return asin(div(i, sqrt(_one + mul(i, i))));
	}
}
